package com.sun.dailyprj.network;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Toast;

import com.sun.dailyprj.R;
import com.sun.dailyprj.databinding.ActivityNetworkBinding;
import com.sun.dailyprj.util.Logger;

import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;

import org.json.JSONException;
import org.json.JSONObject;

import okhttp3.Response;


public class NetworkActivity extends AppCompatActivity{

    private ActivityNetworkBinding mBinding;
    private NetWorkBroadcast mNetWork;


    private final static String reqBody = " {\n" +
            "\t\"vin\": \"SIGNIFICANT123456\",\n" +
            "\t\"ecus\": {\n" +
            "\t\t\"tbox\": {\n" +
            "\t\t\t\"name\": \"tbox\",\n" +
            "\t\t\t\"sv\": \"00.00.66_00.00.28\",\n" +
            "\t\t\t\"hv\": \"703000078AA;9KG\",\n" +
            "\t\t\t\"sn\": \"FCOT1A29826U122#\",\n" +
            "\t\t\t\"props\": {\n" +
            "\t\t\t\t\n" +
            "\t\t\t}\n" +
            "\t\t},\n" +
            "\t\t\"dmc\": {\n" +
            "\t\t\t\"name\": \"dmc\",\n" +
            "\t\t\t\"sv\": \"g6sh_t1d-userdebug 9 PPR2.181005.003 eng.uidp45.20200416.145055 dev-keys\",\n" +
            "\t\t\t\"hv\": \"UNKNOW;9BJ\",\n" +
            "\t\t\t\"sn\": \"UNKNOW\",\n" +
            "\t\t\t\"props\": {\n" +
            "\t\t\t\t\n" +
            "\t\t\t}\n" +
            "\t\t},\n" +
            "\t\t\"cwc\": {\n" +
            "\t\t\t\"name\": \"cwc\",\n" +
            "\t\t\t\"sv\": \"00.00.00_00.01.14\",\n" +
            "\t\t\t\"hv\": \"802000055AA;9LC\",\n" +
            "\t\t\t\"sn\": \"\",\n" +
            "\t\t\t\"props\": {\n" +
            "\t\t\t\t\n" +
            "\t\t\t}\n" +
            "\t\t}\n" +
            "\t}\n" +
            "}";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_network);
        mBinding.setNet(this);

        testUI();
        testNetWorkChange();
    }

    private void testNetWorkChange() {
        IntentFilter filter = new IntentFilter();
        filter.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        mNetWork = new NetWorkBroadcast();
        registerReceiver(mNetWork,filter);
    }

    private class NetWorkBroadcast extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            Logger.warn("action : " + action);
            ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo activeInfo = manager.getActiveNetworkInfo();
            //如果无网络连接activeInfo为null

            //也可获取网络的类型
            if(activeInfo != null) { //网络连接
                Toast.makeText(context, "测试：网络连接成功", Toast.LENGTH_SHORT).show();
                Logger.warn("测试：网络连接成功");
            } else { //网络断开
                Toast.makeText(context, "测试：网络断开", Toast.LENGTH_SHORT).show();
                Logger.warn("测试：网络断开");
            }

        }
    }

    public void onNetTestClick(View v) {
        new Thread(new Runnable(){
            @Override
            public void run() {
                String url = "https://58.243.176.166:8443/v0/vconnect";
                try{
                    JSONObject obj = new JSONObject(reqBody);
                    if(obj == null) {
                        Logger.warn("obj is null");
                        return;
                    }
                    Response resp = OkHttpUtil.getInstance().postData(url, null,
                            new JSONObject(reqBody));
                    Logger.warn("resp.code:" + resp.code() + " ; resp.body:" + resp.body());
                } catch (JSONException e) {
                    e.printStackTrace();
                    Logger.error("err:" + e.toString());
                }
            }
        }).start();
    }

    private void testUI() {
        String msg = "5月14日13：00";
        String content = this.getResources().getString(R.string.chery_btn_book_already);
        content = String.format(content, msg);
        int len = content.length();
        SpannableString spanString = new SpannableString(content);
        ForegroundColorSpan span =
                new ForegroundColorSpan(getResources().getColor(R.color.color_disable));
        ForegroundColorSpan white = new ForegroundColorSpan(getResources().getColor(R.color.color_white));
        spanString.setSpan(span, 0, 5, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spanString.setSpan(white, 5, len, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        mBinding.textColor.setText(spanString);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mNetWork);
    }
}
